package com.game.demo409.demo409;
import com.actionbarsherlock.app.SherlockFragmentActivity;

import com.actionbarsherlock.app.ActionBar;

import android.content.Intent;
import android.support.v4.app.Fragment;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;

import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.support.v4.widget.DrawerLayout;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.game.demo409.demo409.R;
import com.ikimuhendis.ldrawer.ActionBarDrawerToggle;
import com.ikimuhendis.ldrawer.DrawerArrowDrawable;

import java.util.HashMap;
import java.util.ArrayList;
import java.util.Map;
import java.util.List;


import fragments.FindingFragment;
import fragments.HomeFragment;
import fragments.NavigationFragment;
import fragments.SettingFragment;


public class MainActivity extends SherlockFragmentActivity {

    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;
    private DrawerArrowDrawable drawableArrow;
    private LinearLayout mDrawerLinear;

    private String[] mActionbarTitle;
    private CharSequence mTitle;


    private Context mContext;
    private FragmentManager mFragmentManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActionBar actionbar = this.getSupportActionBar();
        actionbar.setDisplayHomeAsUpEnabled(true);
        actionbar.setHomeButtonEnabled(true);

        mContext = this.getApplicationContext();
        mFragmentManager = getSupportFragmentManager();

        mTitle = this.getTitle();
        mActionbarTitle = this.getResources().getStringArray(R.array.menu_array);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerLinear =(LinearLayout)findViewById(R.id.drawer_linear);
        mDrawerList = (ListView) findViewById(R.id.drawer_list);

        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

        mDrawerList.setAdapter(new SimpleAdapter(this,
                this.getMenuArrayList(),
                R.layout.menu_list_item,
                new String[]{"menu_icon", "menu_title"},
                new int[]{R.id.menu_icon,
                        R.id.menu_titile}));
        drawableArrow = new DrawerArrowDrawable(this) {
            @Override
            public boolean isLayoutRtl() {
                return false;
            }
        };

        mDrawerToggle = new ActionBarDrawerToggle(this,
                mDrawerLayout,
                drawableArrow,
                R.string.drawer_open,
                R.string.drawer_close) {

            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                invalidateOptionsMenu();
            }

            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                invalidateOptionsMenu();
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);
        mDrawerToggle.syncState();
        if (savedInstanceState == null) {
            selectItem(0);
        }
    }
//    @Override
//    public boolean onPrepareOptionsMenu(com.actionbarsherlock.view.MenuItem menu) {


//        return super.onPrepareOptionsMenu(menu);
//    }

    @Override
    public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
        getSupportMenuInflater().inflate(R.menu.message,
                menu);

        return     super.onCreateOptionsMenu(menu);

    }

    @Override
    public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
       switch (item.getItemId()) {
           case android.R.id.home:
               if (mDrawerLayout.isDrawerOpen(mDrawerLinear)) {
                   mDrawerLayout.closeDrawer(mDrawerLinear);
               } else {
                   mDrawerLayout.openDrawer(mDrawerLinear);
               }
               break;
           case R.id.menu_message:
               Intent intent = new Intent(MainActivity.this, com.game.demo409.demo409.MessageActivity.class);
               startActivity(intent);
               break;
           default:
               break;

       }

//        if (item.getItemId() == android.R.id.home) {
//            if (mDrawerLayout.isDrawerOpen(mDrawerLinear)) {
//                mDrawerLayout.closeDrawer(mDrawerLinear);
//            } else {
//                mDrawerLayout.openDrawer(mDrawerLinear);
//            }
//        }
//        else if(item.getItemId() == R.menu.message) {
//            Intent intent = new Intent(MainActivity.this, com.game.demo409.demo409.MessageActivity.class);
//            startActivity(intent);
//        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }
    public class DrawerItemClickListener implements AdapterView.OnItemClickListener{
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                                long id) {
            selectItem(position);
        }

    }

    public  void selectItem(int position) {
        switch (position) {
            case 0:
                changeFragment(new HomeFragment());
                break;
            case 1:
                changeFragment(new NavigationFragment());
                break;
            case 2:
                changeFragment(new FindingFragment());
                break;
            case 3:
                changeFragment(new SettingFragment());
                break;
            default:
                break;
            }
        mDrawerList.setItemChecked(position, true);
        setTitle(mActionbarTitle[position]);
        mDrawerLayout.closeDrawer(mDrawerLinear);
        }
    public void changeFragment (Fragment targetfragment) {

       mFragmentManager.beginTransaction().replace(R.id.main_content,targetfragment).setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE).commit();

    }
    @Override
    public void setTitle(CharSequence title) {
        mTitle = title;
        getActionBar().setTitle(mTitle);
    }
    public List<Map<String, Object>> getMenuArrayList() {
        List<Map<String, Object>> menuList = new ArrayList<Map<String, Object>>();
        String[] menuTitles = this.getResources().getStringArray(R.array.menu_array);

        Map<String, Object> menuMap = new HashMap<String, Object>();

        menuMap.put("menu_icon", R.drawable.pin);
        menuMap.put("menu_title", menuTitles[0]);
        menuList.add(menuMap);

        menuMap = new HashMap<String, Object>();
        menuMap.put("menu_icon", R.drawable.map);
        menuMap.put("menu_title", menuTitles[1]);
        menuList.add(menuMap);


        menuMap = new HashMap<String, Object>();
        menuMap.put("menu_icon", R.drawable.eye);
        menuMap.put("menu_title", menuTitles[2]);
        menuList.add(menuMap);

        menuMap = new HashMap<String, Object>();
        menuMap.put("menu_icon", R.drawable.settings);
        menuMap.put("menu_title", menuTitles[3]);
        menuList.add(menuMap);

        return menuList;
    }

}
